package com.shinvent.dao;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;

import com.shinvent.common.Constants;
import com.shinvent.common.Logger;

public class BaseDao {
	
	Connection connection = null;
	Statement statement = null;
	ResultSet rs = null;
	String tableName = null;
	String primaryKey = null;

	public BaseDao(String tableName, String primaryKey){
		try{
			Class.forName("org.sqlite.JDBC");
			connection = DriverManager.getConnection("jdbc:sqlite:" + Constants.DATABASE);
			statement = connection.createStatement();
			
			//statement.setQueryTimeout(30);
			//rs = null;
			this.primaryKey = primaryKey;
			this.tableName = tableName;
		}catch(Exception e){
			e.printStackTrace();
			Logger.logError(e);
		}	
	}
	
	public void update(String columnName, Object value, String keyValue) throws Exception{
		try{
			statement.executeUpdate("UPDATE " + tableName + " SET " + columnName + " = '" + value + "' WHERE " + primaryKey + " = " + keyValue );
		}catch(Exception e){
			e.printStackTrace();
			Logger.logError(e);
			throw new Exception();
		}
	}
	
	public boolean insert(String keyValue){
		try{
			statement.executeUpdate("INSERT INTO " + tableName + "(" + primaryKey +  ") VALUES (" + keyValue + ")");
			return true;
		}catch(Exception e){
			e.printStackTrace();
			Logger.logError(e);
			return false;
		}
	}
	
	public boolean delete(){
		try{
			if(rs.getObject(primaryKey)!=null){
				statement.executeUpdate("DELETE FROM " + tableName + " WHERE " + primaryKey +  " = '" + rs.getString(primaryKey) + "'");
			}
			return true;
		}catch(Exception e){
			e.printStackTrace();
			Logger.logError(e);
			return false;
		}
	}
	
	public void query(){
		query(null);
	}
	
	public void query(String sql){
		query(sql, null);
	}
	
	public void query(String sql, String orderBy){
		try{
			
			if(orderBy != null && !orderBy.equals("")){
				if(sql != null && !sql.equals("")){
					rs = statement.executeQuery("SELECT * FROM " + tableName + " WHERE " + sql + " ORDER BY " + orderBy);
				}else{
					rs = statement.executeQuery("SELECT * FROM " + tableName + " ORDER BY " + orderBy);
				}
			}else{
				if(sql != null && !sql.equals("")){
					rs = statement.executeQuery("SELECT * FROM " + tableName + " WHERE " + sql);
				}else{
					rs = statement.executeQuery("SELECT * FROM " + tableName);
				}
			}
			
		}catch(Exception e){
			System.out.println(e.getMessage());
			Logger.logError(e);
			throw new RuntimeException(e);
		}
	}
	
	public Object getProperty(String columnName) throws SQLException{
		return rs.getObject(columnName);
	}
	
	public String getString(String columnName) throws SQLException{
		return rs.getString(columnName);
	}
	
	public BigDecimal getBigDecimal(String columnName) throws SQLException{
		return rs.getBigDecimal(columnName);
	}
	
	public Timestamp getTimestamp(String columnName) throws SQLException{
		return rs.getTimestamp(columnName);
	}
	
	public boolean gotoNext(){
		try{
			if(rs.next()){
				return true;
			}else{
				return false;
			}
		}catch(Exception e){
			return false;
		}
	}
	
	public boolean gotoFirst(){
		try{
			if(rs.first()){
				return true;
			}else{
				return false;
			}
		}catch(Exception e){
			return false;
		}
	}
	
	public boolean gotoLast(){
		try{
			if(rs.last()){
				return true;
			}else{
				return false;
			}
		}catch(Exception e){
			return false;
		}
	}
	
	public void close(){
		try{
			this.connection.close();
		}catch(Exception e){
			//do nothing
		}
		
		try{
			this.statement.close();
		}catch(Exception e){
			//do nothing
		}
		
		try{
			this.rs.close();
		}catch(Exception e){
			//do nothing
		}
	}
	
}
